package cn.iocoder.yudao.module.crm.dal.dataobject.performance.person;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import org.apache.tika.utils.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * CRM_业绩配置 DO
 *
 * @author 超级管理员
 */
@TableName("crm_person_performance_config_detail")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PersonPerformanceConfigDetailDO extends BaseDO {

    /**
     * 数据id
     */
    @TableId
    private Long id;
    /**
     * 员工id
     * 关联system_user.id
     */
    private Long userId;
    /**
     * 指标点
     */
    private String performancePoint;
    /**
     * 所属年月
     */
    private String belongMonth;
    /**
     * 目标值
     */
    private String targetValue;

    /**
     * 完成值
     */
    @TableField(exist = false)
    private String finishValue;
    /**
     * 完成率
     */
    @TableField(exist = false)
    private String finishRate;


    public void updateFinishRate(){
        if(StringUtils.isBlank(targetValue)){
            finishRate = "100%";
        }else if(StringUtils.isBlank(finishValue)){
            finishRate = "0%";
        }else{
            Pattern testNumberPattern = Pattern.compile("^[-+]?[0-9]+(.[0-9]+)?$");
            if(!testNumberPattern.matcher(finishValue).matches()){
                finishRate = "完成值不为数值数据，无法计算";
            }else if( !testNumberPattern.matcher(targetValue).matches()) {
                finishRate = "目标值不为数值数据，无法计算";
            }else{
                BigDecimal targetBigDecimal = new BigDecimal(targetValue);
                BigDecimal finishBigDecimal = new BigDecimal(finishValue);
                BigDecimal rateBigDecimal = finishBigDecimal.multiply(BigDecimal.valueOf(100)).divide(targetBigDecimal,2,BigDecimal.ROUND_UP);
                finishRate = rateBigDecimal.toString() + "%";
            }

        }
    }


}